package com.jiaai.cloud.wx.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jiaai.cloud.base.entity.RestResultGenerator;
import com.jiaai.cloud.base.exception.ServiceException;
import com.jiaai.cloud.wx.entity.TWxalter;
import com.jiaai.cloud.wx.entity.TWxalterimg;
import com.jiaai.cloud.wx.service.ITWxalterService;
import com.jiaai.cloud.wx.service.ITWxalterimgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 佳爱网络科技
 * @since 2017-09-08
 */
@Controller
@RequestMapping("/api/v1/twxalter")
public class TWxalterController {

    @Autowired
    private ITWxalterService itWxalterService;
    @Autowired
    private ITWxalterimgService itWxalterimgService;

    /**
     * 获取所有的轮播组
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/getaltergroup",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object getAlterGroup() throws ServiceException {
        List<TWxalter> list = itWxalterService.selectList(null);
        try {
            return RestResultGenerator.genSuccessResult(list);
        } catch (Exception e){
            throw new ServiceException("获取失败", e.getMessage());
        }
    }

    /**
     * 获取单个轮播组的信息
     * @param id
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/getalterone/{alterid}",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object getAlterOne(@PathVariable("alterid") Integer id) throws ServiceException {
        List<TWxalter> list = itWxalterService.getAlterGroupOne(id);
        try {
            return RestResultGenerator.genSuccessResult(list);
        } catch (Exception e){
            throw new ServiceException("获取失败", e.getMessage());
        }
    }

    /**
     * 编辑单个轮播组的状态
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object setAlterState(@RequestBody TWxalter tWxalter) throws ServiceException {
        try {
            itWxalterService.updateById(tWxalter);
            return RestResultGenerator.genSuccessResult("编辑成功");
        } catch (Exception e){
            throw new ServiceException("编辑失败", e.getMessage());
        }
    }

    /**
     * 添加新的轮播组
     * @param tWxalter
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object add(@RequestBody TWxalter tWxalter) throws ServiceException {
        try {
            itWxalterService.insert(tWxalter);
            return RestResultGenerator.genSuccessResult("添加成功");
        } catch (Exception e){
            throw new ServiceException("添加失败", e.getMessage());
        }
    }

    /**
     * 删除轮播组及其组下的alterimg
     * @param id
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    @Transactional
    public Object delete(@PathVariable("id") Integer id) throws ServiceException {
        try {
            itWxalterimgService.delete(new EntityWrapper<TWxalterimg>().eq("altergroupid", id));
            itWxalterService.deleteById(id);
            return RestResultGenerator.genSuccessResult("删除成功");
        } catch (Exception e){
            throw new ServiceException("删除失败", e.getMessage());
        }
    }

    /**
     * 轮播组下新增轮播图片
     * @param tWxalterimg
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/updateadd",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object updateAdd(@RequestBody TWxalterimg tWxalterimg) throws ServiceException {
        try {
            itWxalterimgService.insert(tWxalterimg);
            return RestResultGenerator.genSuccessResult("添加成功");
        } catch (Exception e){
            throw new ServiceException("添加失败", e.getMessage());
        }
    }


}
